package com.lwt.spock

import com.lwt.spock.Controller.OtherController
import com.lwt.spock.pojo.Other
import com.lwt.spock.service.OtherService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Shared
import spock.lang.Specification

/**
 * @author lwt* @Description
 * @date  2021/8/15 21:23
 * @version 1.0
 */
// 使用Spock测试框架框架就不能使用下面的注解否则报错No runnable methods
//@RunWith(SpringRunner.class)
// 如果不RANDOM_PORT的话TestRestTemplate注入时会异常NoSuchBeanDefinitionException
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class OtherSpockTest extends Specification{

    @Autowired
    OtherController otherController
    @Autowired
    OtherService otherService

    void setup(){
        otherService = Mock(OtherService)
        otherController.otherService = otherService
    }

    def "test/getOtherById"(){
        given: "定义请求数据格式"
        def other = new Other()
        other.setId(1)
        other.setName("myName")
        other.setEnabled(true)

        // and 准备数据
        and: "mock掉接口返回的用户信息"
        otherService.getOtherById(_) >> other

        when: "向接口发请求"
        def response = otherController.getOtherById(1)
        // then 判断是否符合预期
        then: '验证返回结果是否符合预期值'
        with(response){
            id == 1
            name == "myName"
        }

    }

}
